package com.djnt.catering;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.TreeMap;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class myTabActivity extends TabActivity {
	protected class Item {
		public Item(TabSpec s, Class<?> cls2) {
			// TODO Auto-generated constructor stub
			this.tabSpec = s;
			this.cls = cls2;
		}

		TabSpec tabSpec;
		Class<?> cls;
	}

	protected TreeMap<String, Item> mapTabSpec = new TreeMap<String, Item>();

	void addMainTab(String tag, String text, int resId, Class<?> cls) {
		// TODO Auto-generated method stub
		TabHost tab = this.getTabHost();
		TabSpec s = tab.newTabSpec(tag);
		Resources res = this.getResources();
		s.setIndicator(text, res.getDrawable(resId));
		s.setContent(new Intent(this, cls));
		tab.addTab(s);
		mapTabSpec.put(tag, new Item(s, cls));
	}

	void addSubTab(String tag, String text, Class<?> cls) {
		// TODO Auto-generated method stub
		TabHost tab = this.getTabHost();
		TabSpec s = tab.newTabSpec(tag);
		LayoutInflater mInflater = LayoutInflater.from(this);
		View view = mInflater.inflate(R.layout.main_tab_indicator, null);
		TextView title = (TextView) view.findViewById(R.id.title);
		title.setText(text);
		s.setIndicator(view);
		s.setContent(new Intent(this, cls));
		tab.addTab(s);
		mapTabSpec.put(tag, new Item(s, cls));
	}

	void changeContent(String tag, Class<?> cls) {
		Item t = this.mapTabSpec.get(tag);
		t.tabSpec.setContent(new Intent(this, cls));
		TabHost tabhost = this.getTabHost();
		int i = tabhost.getCurrentTab();
		tabhost.clearAllTabs();
		for (Entry<String, Item> s : mapTabSpec.entrySet()) {
			tabhost.addTab(s.getValue().tabSpec);
		}
		tabhost.setCurrentTab(i);
	}

	void changeContent(Context context, Class<?> newClass) {
		TabHost tabhost = this.getTabHost();
		int i = tabhost.getCurrentTab();
		tabhost.clearAllTabs();
		for (Entry<String, Item> s : mapTabSpec.entrySet()) {
			String k = s.getKey();
			Item v = s.getValue();
			if (v.cls.equals(context.getClass())) {
				v.tabSpec.setContent(new Intent(this, newClass));
				v.cls = newClass;
			}
			tabhost.addTab(v.tabSpec);
		}
		tabhost.setCurrentTab(i);
	}
}
